home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / EmployeeFileAccessor.java < prev    next >
Text File  |  1998-11-01  |  4KB  |  169 lines

  1. import java.io.*;
  2. /**
  3.  * Provide file storage and retrieval of employee data objects.
  4.  */
  5.  
  6. public class EmployeeFileAccessor
  7. {
  8.     RandomAccessFile f;
  9.  
  10.     /**
  11.      * Open the employee file; create it if it does not exist.
  12.      */
  13.  
  14.     public EmployeeFileAccessor(String fname)
  15.     throws IOException
  16.     {
  17.         f = new RandomAccessFile(fname, "rw");
  18.             //{{INIT_CONTROLS
  19.         //}}
  20. }
  21.  
  22.     /**
  23.      * Set the employee file to the beginning
  24.      */
  25.  
  26.     public void reset()
  27.     throws IOException
  28.     {
  29.         f.seek(0);
  30.     }
  31.  
  32.     // The size, in bytes, of an employee record. Records are fixed-size, to
  33.     // allow for easy modification.
  34.  
  35.     final static int recsize =
  36.         Name.MAX_LENGTH+1 + Section.MAX_LENGTH+1 + SSN.MAX_LENGTH+1 +1;
  37.  
  38.     /**
  39.      * Write a new employee to the end of the file
  40.      */
  41.  
  42.     public void addEmployee(Employee e)
  43.     throws IOException
  44.     {
  45.         // Transfer field values to buffer
  46.         byte[] b = new byte[recsize];
  47.         int pos = 0;
  48.  
  49.         int len = e.name.value().length();
  50.         byte[] temp = e.name.value().getBytes();
  51.         inputBytes(temp, b, pos);
  52.         b[pos + len] = '\0';
  53.         pos += Name.MAX_LENGTH + 1;
  54.  
  55.         String a = String.valueOf(e.section.value());
  56.         len = a.length();
  57.         temp = a.getBytes();
  58.         inputBytes(temp, b, pos);
  59.         b[pos + len] = '\0';
  60.         pos += Section.MAX_LENGTH + 1;
  61.  
  62.         len = e.ssn.value().length();
  63.         temp = e.ssn.value().getBytes();
  64.         inputBytes(temp, b, pos);
  65.         b[pos + len] = '\0';
  66.  
  67.         b[recsize-1] = '\n';
  68.  
  69.         // Write buffer to file, as a fixed-size record
  70.         f.seek(f.length());
  71.         f.write(b);
  72.     }
  73.     
  74.     public void inputBytes(byte[] from, byte[] to, int at) {
  75.         int len = from.length;
  76.         for (int i = 0; i < len; ++i) {
  77.             to[at + i] = from[i];
  78.         }
  79.     }
  80.     /**
  81.      * Fetch the first or next employee from the file.
  82.      */
  83.  
  84.     public Employee getNextEmployee()
  85.     throws IOException
  86.     {
  87.         byte[] b = new byte[recsize];
  88.         int nchars;
  89.         System.out.println("About to read");
  90.         if ((nchars = f.read(b)) <= 0) return null;
  91.         System.out.println("Read " + nchars + " chars");
  92.         int pos = 0;
  93.         int end = Name.MAX_LENGTH + 1;
  94.         String data = new String(b);
  95.         String n = "";
  96.         String a = "";
  97.         String s = "";
  98.         try {
  99.             n = data.substring(pos, end);
  100.             n = n.trim();
  101.             System.out.println("Fetched name='" + n + "'");
  102.             pos = end;
  103.             end += Section.MAX_LENGTH + 1;
  104.             a = data.substring(pos, end);
  105.             a = a.trim();
  106.             System.out.println("Fetched section=" + a);
  107.             pos = end;
  108.             end += SSN.MAX_LENGTH + 1;
  109.             s = data.substring(pos, end);
  110.             s = s.trim();
  111.             System.out.println("Fetched ssn=" + s);
  112.         }
  113.         catch (StringIndexOutOfBoundsException ex) {
  114.             System.out.println("Invalid employee read from file!");
  115.             return null;
  116.         }
  117.  
  118.         Employee emp;
  119.         try
  120.         {
  121.             emp = new Employee(n, a, s);
  122.         }
  123.         catch (InvalidEmployeeException e)
  124.         {
  125.             System.out.println("Invalid employee read from file!");
  126.             return null;
  127.         }
  128.  
  129.         return emp;
  130.     }
  131.  
  132.     /**
  133.      * Read the employee file, and build a list of employee objects.
  134.      */
  135.  
  136.     EmployeeList buildEmployeeList()
  137.     {
  138.         EmployeeList elist = new EmployeeList();
  139.         Employee emp;
  140.         int noOfEmployees = 0;
  141.         try
  142.         {
  143.             reset();
  144.         }
  145.         catch (IOException ex)
  146.         {
  147.             return elist;
  148.         }
  149.         for (;;)
  150.         {
  151.             try
  152.             {
  153.                 emp = getNextEmployee();
  154.             }
  155.             catch (IOException ex)
  156.             {
  157.                 return elist;
  158.             }
  159.             if (emp == null) break;
  160.             noOfEmployees++;
  161.             elist.add(emp);
  162.         }
  163.         System.out.println("noOfEmployees=" + noOfEmployees);
  164.         return elist;
  165.     }
  166.     //{{DECLARE_CONTROLS
  167.     //}}
  168. }
  169.